/* uedb_sqlite3.h */
#ifndef UE_DB_SQLITE3_H
#define UE_DB_SQLITE3_H

#include "sqlite3.h"

#pragma pack(push, 1)

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

typedef struct
{
	char          folder[MAX_PATH + 1];                  /* folder name to store sqlite3 DB */
	char          dbname[MAX_DB_NAME_LEN + 1];           /* DB name (sqlite3 file name) */
	sqlite3      *pdb;                                   /* pointer to sqlite3 object */
	char        **result;                                /* last query result include column names */
	int           rows;                                  /* row number in last query result */
	int           columns;                               /* column number in last query result */
}sqlite3_connection;

/* If dbname not specified, check if the specified folder exist and can be read/write; 
 * Otherwise check if the specified db can be open success.
 * @param[in] folder The specified folder path that sqlite3 DB will store in.
 * @param[in] dbname The database name (file name) to be open.
 * @param[out] pconnection Pointer to sqlite3_connection structure.
 * @return UE_DB_ERR_SUCCESS indicate success, otherwise some error occur.
 */
int uedb_sqlite3_connect(const char *folder, const char *dbname, sqlite3_connection *pconnection);

/* Create tables and initialize a new UE DB.
 * @param[in] dbspec The database specification.
 * @param[in] pconnection The database connection for the new create database.
 * @return UE_DB_ERR_SUCCESS indicate success, otherwise some error occur.
 */
int uedb_sqlite3_create(const char *dbspec, sqlite3_connection *pconnection);

/* Close and open sqlite3 connection.
 * @param pconnection Pointer to sqlite3_connection structure.
 * @return UE_DB_ERR_SUCCESS indicate success, otherwise some error occur.
 */
int uedb_sqlite3_close(sqlite3_connection *pconnection);

/* Execute specified SQL statement for non-query purpose.
 * @param[in] sql The SQL statement.
 * @param[in] params The array of parameters
 * @param[in] param_num Parameter number
 * @param[in] pconnection The database connection for the new create database.
 * @return UE_DB_ERR_SUCCESS indicate success, otherwise some error occur.
 */
int uedb_sqlite3_execute(const char *sql, sqlite3_connection *pconnection);
int uedb_sqlite3_execute_ex(const char *sql, const uedb_parameter *params, const unsigned int param_num, sqlite3_connection *pconnection);

/* Execute specified SQL statement for query purpose.
 * @param[in] sql The SQL statement.
 * @param[in/out] pconnection The database connection for execute the sql query.
 * @return UE_DB_ERR_SUCCESS indicate success, otherwise some error occur.
 */
int uedb_sqlite3_query(const char *sql, sqlite3_connection *pconnection);

/* Close last query result.
 * @param[in/out] pconnection The pointer to sqlite3_connection.
 * @return UE_DB_ERR_SUCCESS indicate success, otherwise some error occur.
 */
int uedb_sqlite3_close_query(sqlite3_connection *pconnection);

/* Get the row number in last query result
 * @param[in] pconnection The pointer to sqlite3 connection.
 * @param[out] prows Pointer to an integer to receive the row number
 * @return UE_DB_ERR_SUCCESS indicate success, otherwise some error occur.
 */
int uedb_sqlite3_rows(sqlite3_connection *pconnection, int *prows);

/* Get specified field value in last query result
 * @param[in] column_name The name of the column to get result
 * @param[in] row The index of the row to get the result
 * @param[in] pconnection The pointer to sqlite3 connection.
 * @param[out] presult Pointer to a string to receive the result
 * @return UE_DB_ERR_SUCCESS indicate success, otherwise some error occur.
 */
int uedb_sqlite3_result_by_name(const char *column_name, const int row, sqlite3_connection *pconnection, const char **presult);

/* Get specified field value in last query result
 * @param[in] column The index of the column to get result
 * @param[in] row The index of the row to get the result
 * @param[in] pconnection The pointer to sqlite3 connection.
 * @param[out] presult Pointer to a string to receive the result
 * @return UE_DB_ERR_SUCCESS indicate success, otherwise some error occur.
 */
int uedb_sqlite3_result_by_index(const int column, const int row, sqlite3_connection *pconnection, const char **presult);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#pragma pack(pop)

#endif /* UE_DB_SQLITE3_H */